var database = require('../../data/data.js');
Page({
  data: {
  },
  onLoad: function () {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    var that = this;
    wx.login({
      success(res) {
        var code = res.code;
        wx.request({
          url: database.url + "/api/V2/user/access_token",
          data: {
            clientId: "QqKTO5BTPN6REI26QAgq",
            clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
          },
          success(res) {
            database.accessToken = res.data.data.accessToken;
            if (database.unionid == "") {
              wx.request({
                url: database.url + '/api/V2/integral/integral_openid',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  accessToken: database.accessToken,
                  weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                  regionName: "other"
                },
                success(res) {
                  var sessoionKey = res.data.data.session_key;
                  wx.getUserInfo({
                    data: {
                      withCredentials: true,
                      lang: "zh_CN"
                    },
                    success(res) {
                      wx.request({
                        url: database.url + '/api/V2/user/we_chat_user',
                        method: "POST",
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                          accessToken: database.accessToken,
                          weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                          encryptedData: res.encryptedData,
                          iv: res.iv,
                          sessionKey: sessoionKey,
                          weChatName: "场馆小程序"
                        },
                        success(res) {
                          database.openid = res.data.data.openId;
                          database.unionid = res.data.data.unionId;
                          wx.request({
                            url: database.url + '/api/V2/user/verify_wechat_small',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              unionid: database.unionid
                            },
                            success(res) {
                              wx.getLocation({
                                success: function (res) {
                                  database.latitude = res.latitude;
                                  database.longitude = res.longitude;
                                  that.setData({
                                    url: database.url + "/front/wechat/stadium/list?unionId=" + database.unionid + "&latitude=" + database.latitude + "&longitude=" + database.longitude
                                  })
                                },
                              })
                              if (res.data.data.verify == "yes") {
                                database.userId = res.data.data.id
                                database.login = true;
                              } else {
                                database.login = false;
                              }
                              wx.hideLoading();
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            } else {
              wx.getLocation({
                success: function (res) {
                  database.latitude = res.latitude;
                  database.longitude = res.longitude;
                  that.setData({
                    url: database.url + "/front/wechat/stadium/list?unionId=" + database.unionid + "&latitude=" + database.latitude + "&longitude=" + database.longitude
                  })
                },
              })
              wx.hideLoading();
            }
          }
        })
      }
    })
  },
  onShow() {
    wx.showLoading({
      title: '资源加载中',
    })
    var that = this;
    if (database.payFlag) {
      wx.request({
        url: database.url + '/api/V2/stadium/cancel_order',
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          accessToken: database.accessToken,
          unionId: database.unionid,
          stadiumId: database.stadiumId
        },
        success(res) {
          wx.hideLoading();
        }
      })
      database.payFlag = false;
    } else {
      wx.hideLoading();
    }
    if (database.homeFlag) {
      database.homeFlag = false;
      var timestamp = new Date().getTime();
      this.setData({
        url: database.url + "/front/wechat/stadium/list?unionId=" + database.unionid + "&latitude=" + database.latitude + "&longitude=" + database.longitude + "&timesTamp=" + timestamp
      })
    }
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '体育场馆空闲状态在线预订',
      path: '/pages/home/home',
      imageUrl: '/img/homeShare.png',
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})
